package it.tcm.TeacherClassMate.action;


import java.util.List;

import it.tcm.TeacherClassMate.beans.OrderMainBean;
import it.tcm.TeacherClassMate.beans.SessionUser;
import it.tcm.TeacherClassMate.beans.UserBean;
import it.tcm.TeacherClassMate.facade.OrderMainManageBase;
import it.tcm.TeacherClassMate.facade.UserManageBase;
import it.tcm.TeacherClassMate.util.Util;
import it.tcm.TeacherClassMate.writer.DocxWriter;
import it.tcm.TeacherClassMate.writer.ResultWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;



public class GoToDownloadAction extends Action {

	// --------------------------------------------------------- Instance Variables

	// --------------------------------------------------------- Methods


	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * 
	 */

	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) 
	{		
		 logger.info("GO TO DOWNLOAD ACTION");
		 OrderMainBean omb = (OrderMainBean)request.getSession().getAttribute(Util.ORDER_MAIN_OBJECT);
		 omb.setFormulaNo(omb.getOrderRows().size());
		 omb.setCompleted(1);
		 omb.setSaved(1);
		 OrderMainManageBase ommb = new OrderMainManageBase();
		 ommb.save(omb);
		 // scalo una verifica
		 UserManageBase userManage = new UserManageBase();		
		 SessionUser sessionUser = (SessionUser) request.getSession().getAttribute("sessionUser");
		 UserBean user = userManage.getUserBeanByUsername(sessionUser.getUsername());
		 user.setVerifNum(user.getVerifNum()-1);
		 userManage.save(user);
		 DocxWriter dw = new DocxWriter();
		 dw.createDocx(omb);
		 
		 ResultWriter rw = new ResultWriter();
		 rw.createResult(omb);
		 
		
		 // creo il documento dei risultati
		 return mapping.findForward("gotodownload"); 
		 
	}
	
	
}